package com.berwin.network;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.berwin.listener.SocketEventListener;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class Session implements Runnable {

    public static final int MSG_TYPE_SYNC_DEVICE = 0; // 同步设备名消息
    public static final int MSG_TYPE_SYNC_TEXT = 1; // 同步文本消息
    public static final int MSG_TYPE_SYNC_TEXTS = 2; // 同步所有文本消息
    public static final int MSG_TYPE_SYNC_FILE = 3; // 同步文件消息
    public static final int MSG_TYPE_SYNC_FILES = 5; // 同步所有文件消息
    public static final int MSG_TYPE_CONNECT = 10; // 连接消息
    public static final int MSG_TYPE_DISCONNECT = 11; // 断开连接消息

    private String ip;
    private int port;
    private Socket socket;
    private boolean isRunning;
    private SocketEventListener listener;

    public Session(String ip, int port, SocketEventListener listener) {
        this.ip = ip;
        this.port = port;
        this.listener = listener;
    }

    public void start() {
        new Thread(this).start();
    }

    public void send(final String text) {
        if (!this.isRunning || this.socket == null)
            return;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    bufferedWriter.write(text + "\n");
                    bufferedWriter.flush();
                    Log.i("Session", "发送数据:" + text);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    @Override
    public void run() {
        try {
            socket = new Socket(ip, port);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        this.isRunning = true;
        JSONObject objConnect = new JSONObject();
        objConnect.put("type", MSG_TYPE_SYNC_DEVICE);
        String brand = android.os.Build.BRAND;
        String model = android.os.Build.MODEL;
        objConnect.put("device", brand + " " + model);
        this.send(objConnect.toJSONString());
        this.listener.onConnect(this);
        while (this.isRunning) {
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String dataStr = bufferedReader.readLine();
                if (dataStr == null)
                    break;
                Log.i("Session", "收到消息:" + dataStr);
                JSONObject obj = JSONObject.parseObject(dataStr);
                this.listener.onMessage(this, obj);
            } catch (IOException e) {
                this.isRunning = false;
                e.printStackTrace();
            }
        }
        this.isRunning = false;
        this.listener.onDisconnect(this);
        try {
            this.socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.socket = null;
    }

    public void setListener(SocketEventListener listener) {
        this.listener = listener;
    }
}
